<?php
/**
 * Created by PhpStorm.
 * User: 夏浩文
 * Date: 2017/6/14
 * Time: 15:01
 */

namespace app\hongyanche\model;
use \think\Model;
class Coupon extends Model
{
    var $userId;
    var $config;
    public function __construct()
    {
        $this->config = getConfig('product');
        //获取当前user_id
        if (is_null($this->userId)) {
            $userId = $this->userId = model('user')->getUserId();
        } else {
            $userId = $this->userId;
        }
        if (!$userId) {
            return;
        }
    }
    public function add($class)
    {
        //判断是否登录
        if(!$this->userId) return array('state'=>0,'errormsg'=>'请登录');
        $data['create_time'] = time();
        $data['admin_user_id'] = $this->userId;
        $data['way'] = $class;
        $data['state'] = 2;
        $result = db('coupon')->insertGetId($data);
        if(!$result) return array('state'=>0,'errormsg'=>'insert fail');
        return array('state'=>1,'errormsg'=>'ok');
    }
    //获取优惠券列表
    function getList($filter)
    {
        //基础条件
        $type_id_a = $filter['select'];
        if (isset($filter['page']) && $filter['page'] != '') {
            $pages = $filter['page'] - 1;
        } else {
            $pages = 0;
        }
        if (isset($filter['maxPerPage']) && $filter['maxPerPage'] != '') {
            $maxPerPage = $filter['maxPerPage'];
        } else {
            $maxPerPage = 12;
        }
        $order = ["state" => "asc", 'create_time' => 'desc'];
        $where = "state != 0";
        if ($filter['keywords']) {
            $where .= " and (name like '%$filter[keywords]%')";
        }
        //labels条件
        if ($type_id_a) {
            $where .= " and class like '%$type_id_a%'";
        }
        $type_config = db('coupon')->where($where)->order($order)->limit($pages * $maxPerPage, $maxPerPage)->select();
        //获取下一页（page+1）的数据，如果还有就返回more = 1,反之就返回more = 0
        $pages = $pages + 1;
        $type_brand_b = db('coupon')->where($where)->order($order)
            ->limit($pages * $maxPerPage, $maxPerPage)->select();
        if ($type_brand_b) {
            $more = 1;
        } else {
            $more = 0;
        }
        $result = array('state' => 1, 'data' => array('list' => $type_config, 'more' => $more, 'maxPerPage' => $maxPerPage));
        return $result;
    }

//    public function allotCoupon($user_id,$coupon_id)
//    {
//        if(!$user_id){
//            return array('state'=>0,'errormsg'=>'not find user');
//        }
//        if(!$coupon_id){
//            return array('state'=>0,'errormsg'=>'need coupon');
//        }
//        $user_id = explode(',',$user_id);
//        $amount = db('coupon')->where("state = 1 and coupon_id = $coupon_id")->find();
//        if(empty($amount)){
//            return array('state'=>0,'errormsg'=>'not find coupon');
//        }
//        //查询库存是否足够
//        $coupon_order = db('coupon_order')->where("coupon_id = $coupon_id and state = 1")->select();
//        if($amount['amount']==0){
//            return array('state'=>0,'errormsg'=>'优惠券不足');
//        }
//        if(count($user_id)>($amount['amount'] - count($coupon_order))){
//            return array('state'=>0,'errormsg'=>'优惠券不足');
//        }
//        foreach($user_id as $k => $v){
//            $res = db('coupon_order')->where("coupon_id = $coupon_id and user_id = $v")->find();
//            if($res){
//                $user_info = model('user')->getInfoById($v);
//                $user_info['user_name'] = isset($user_info['set_name']) ? $user_info['set_name'] :$user_info['user_name'];
//                return array('state'=>0,'errormsg'=>$user_info['user_name'].'不能重复领');
//            }
//            $data['user_id'] = $v;
//            $data['create_time'] = time();
//            $data['found_user_id'] = $this->userId;
//            $data['coupon_id'] = $coupon_id;
//            db('coupon_order')->insert($data);
//        }
//        return array('state'=>1,'errormsg'=>'ok');
//    }

        //兑换优惠券
    public function getCoupon($data)
    {
        if(!$data['coupon_id']) return ['state'=>0,'errormsg'=>'请选择要兑换的优惠券'];
        if(!$data['amount']) $data['amount'] = 1;

    }
}